# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openjpeg

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}2"
pkgver=2.0.0
pkgrel=2
pkgdesc="An open source JPEG 2000 codec (mingw-w64)"
arch=('any')
url="http://www.openjpeg.org"
license=("BSD")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-lcms2" "${MINGW_PACKAGE_PREFIX}-libtiff" "${MINGW_PACKAGE_PREFIX}-libpng" "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-cmake")
options=('staticlibs' 'strip')
source=("http://openjpeg.googlecode.com/files/${_realname}-${pkgver}.tar.gz"
    0001-fix-install-for-dlls.all.patch
    0002-pkgconfig.all.patch
    0003-versioned-dlls.mingw.patch
    0004-stdio.all.patch)

md5sums=('d9be274bddc0f47f268e484bdcaaa6c5'
         '9df59d9482605662734934fa02129b6c'
         '58b14b2cfe4084b36ff0ee350d1a1189'
         'fe2141975de480664c1d96e365b4b29a'
         'd62d4506616ccfba0bad523a3fee2208')

prepare() {
  cd "$srcdir/${_realname}-$pkgver"
  patch -p1 -i ${srcdir}/0001-fix-install-for-dlls.all.patch
  patch -p1 -i ${srcdir}/0002-pkgconfig.all.patch
  patch -p1 -i ${srcdir}/0003-versioned-dlls.mingw.patch
  patch -p1 -i ${srcdir}/0004-stdio.all.patch
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  
  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null

  [[ -d $srcdir/build-${MINGW_CHOST} ]] && rm -rf $srcdir/build-${MINGW_CHOST}
  mkdir $srcdir/build-${MINGW_CHOST}
  cd $srcdir/build-${MINGW_CHOST}
  
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${PREFIX_WIN} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_TESTING:BOOL=ON \
    -DCMAKE_SYSTEM_PREFIX_PATH=${PREFIX_DEPS} \
    -DOPENJPEG_INSTALL_SUBDIR="openjpeg/${ver}" \
    -DBUILD_MJ2:bool=on \
    -DBUILD_JPWL:bool=on \
    -DBUILD_JPIP:bool=on \
    -DBUILD_JP3D:bool=on \
    ../${_realname}-$pkgver
  make
}

package() {
  cd "$srcdir/build-${MINGW_CHOST}"
  make install

  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libopenjp2.pc \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libopenjp3d.pc \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libopenjpip.pc \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libopenjpwl.pc

}
